package com.example.pwj.ffstudy.ui.gank.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.pwj.ffstudy.R;
import com.example.pwj.ffstudy.base.BaseFragment;
import com.example.pwj.ffstudy.bean.gank.GankBean;
import com.example.pwj.ffstudy.bean.gank.GankData;
import com.example.pwj.ffstudy.bean.gank.GankSection;
import com.example.pwj.ffstudy.ui.gank.adapter.GankAdapter;
import com.example.pwj.ffstudy.ui.gank.component.DaggerGankComponent;
import com.example.pwj.ffstudy.ui.gank.contract.GankContact;
import com.example.pwj.ffstudy.ui.gank.presenter.GankPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by pwj on 2017/8/22.
 */

public class GankFragment extends BaseFragment implements GankContact.View, OnRefreshListener {
    @Inject
    GankPresenter gankPresenter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private GankAdapter gankAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gank;
    }

    @Override
    protected void init() {
        DaggerGankComponent.builder().build().inject(this);
        gankPresenter.takeView(this);
        initRecycleView();
        initRefreshView();
    }

    private void initRecycleView() {
        gankAdapter = new GankAdapter(mContext);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(gankAdapter);
    }

    private void initRefreshView() {
        if (refreshLayout != null){
            refreshLayout.setRefreshHeader(new BezierRadarHeader(mContext));
            refreshLayout.autoRefresh();
            refreshLayout.setEnableLoadmore(false);
            refreshLayout.setOnRefreshListener(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (gankPresenter != null) {
            gankPresenter.dropView();
            gankPresenter = null;
        }
    }

    @Override
    public void loadGankDatas(GankBean gankBean) {
        List<GankSection> gankSections = new ArrayList<>();
        gankSections.add(new GankSection(true, "全部资讯"));
        List<GankData> resultsBeen = new ArrayList<>();
        List<GankData> results = gankBean.getResults();
        for (GankData bean : results) {
            gankSections.add(new GankSection(bean));
        }
        gankAdapter.setDatas(gankSections);
    }

    @Override
    public void loadError(String errorMsg) {
        ToastUtils.showShortSafe(errorMsg);
        refreshLayout.finishRefresh();
    }

    @Override
    public void loadFinish() {
        refreshLayout.finishRefresh();
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        gankPresenter.loadGankDatas(true);
    }
}
